package sample;

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
//import javafx.fxml.FXMLLoader;
import javafx.scene.*;
import javafx.stage.Stage;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.geometry.Insets;
import javafx.scene.text.*;
import javafx.collections.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.event.*;

import java.io.IOException;

public class Main extends Application {

    private ObservableList<FileInfo> fileInfoData = FXCollections.observableArrayList();

    private ObservableList<StorageInfo> storageInfoData = FXCollections.observableArrayList();

    @Override
    public void start(Stage primaryStage) throws Exception{
        //Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));

        Label nodeLabel = new Label("Node Information");
        nodeLabel.setFont(new Font("Arial", 20));

        TableView<StorageInfo> nodeTable = new TableView<>();

        nodeTable.setEditable(false);

        TableColumn nodeNameCol = new TableColumn("NodeName");
        nodeNameCol.setCellValueFactory(new PropertyValueFactory<>("nodeName"));
        TableColumn hostCol = new TableColumn("Host");
        hostCol.setCellValueFactory(new PropertyValueFactory<>("host"));
        TableColumn portCol = new TableColumn("Port");
        portCol.setCellValueFactory(new PropertyValueFactory<>("port"));
        TableColumn volumeCol = new TableColumn("Volume");
        volumeCol.setCellValueFactory(new PropertyValueFactory<>("volume"));
        TableColumn volumeLeftCol = new TableColumn("VolumeLeft");
        volumeLeftCol.setCellValueFactory(new PropertyValueFactory<>("volumeLeft"));
        TableColumn fileNumberCol = new TableColumn("FileNumber");
        fileNumberCol.setCellValueFactory(new PropertyValueFactory<>("fileNumber"));
        TableColumn isOKCol = new TableColumn("IsOK?");
        isOKCol.setCellValueFactory(new PropertyValueFactory<>("isOK"));

        nodeTable.setItems(storageInfoData);

        nodeTable.getColumns().addAll(nodeNameCol, hostCol, portCol, volumeCol, volumeLeftCol, fileNumberCol, isOKCol);

        VBox nodeVBox = new VBox();
        nodeVBox.setSpacing(5);
        nodeVBox.setPadding(new Insets(10, 0, 0, 10));

        Button flushNodeButton = new Button("Flush");
        flushNodeButton.setOnAction((ActionEvent e) -> {
            try{
                Controller.getStorageInfo(storageInfoData);
            }catch (IOException exception) {
                System.out.println("Storage Informaion get failed");
            }
        });

        nodeVBox.getChildren().addAll(nodeLabel, nodeTable, flushNodeButton);



        Label fileLabel = new Label("File Information");
        fileLabel.setFont(new Font("Arial", 20));

        TableView<FileInfo> fileTable = new TableView<>();

        fileTable.setEditable(false);

        TableColumn uuidCol = new TableColumn("UUID");
        uuidCol.setCellValueFactory(new PropertyValueFactory<>("uuid"));
        TableColumn nameCol = new TableColumn("FileName");
        nameCol.setCellValueFactory(new PropertyValueFactory<>("fileName"));
        TableColumn lengthCol = new TableColumn("Length");
        lengthCol.setCellValueFactory(new PropertyValueFactory<>("length"));
        TableColumn mainHostCol = new TableColumn("MainHost");
        mainHostCol.setCellValueFactory(new PropertyValueFactory<>("mainHost"));
        TableColumn mainPortCol = new TableColumn("MainPort");
        mainPortCol.setCellValueFactory(new PropertyValueFactory<>("mainPort"));
        TableColumn backHostCol = new TableColumn("BackHost");
        backHostCol.setCellValueFactory(new PropertyValueFactory<>("backHost"));
        TableColumn backPortCol = new TableColumn("BackPort");
        backPortCol.setCellValueFactory(new PropertyValueFactory<>("backPort"));

        fileTable.setItems(fileInfoData);
        fileTable.getColumns().addAll(uuidCol, nameCol, lengthCol, mainHostCol, mainPortCol, backHostCol, backPortCol);

        VBox fileVBox = new VBox();
        fileVBox.setSpacing(5);
        fileVBox.setPadding(new Insets(10, 0, 0, 10));

        Button flushFileButton = new Button("Flush");
        flushFileButton.setOnAction((ActionEvent e) -> {
            try{
                Controller.getFileInfo(fileInfoData);
            }catch (IOException exception) {
                System.out.println("File informaion get failed");
            }

        });

        fileVBox.getChildren().addAll(fileLabel, fileTable, flushFileButton);

        HBox allHBox = new HBox(15);
        allHBox.getChildren().addAll(nodeVBox,fileVBox);

        Runnable runnable = new Runnable() {
            public void run() {
                while (true) {
                    // ------- code for task to run
                    try{
                        Controller.getFileInfo(fileInfoData);
                        Controller.getStorageInfo(storageInfoData);
                    }catch (IOException e) {
                        System.out.println("Flush failed");
                    }
                    // ------- ends here
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();

        primaryStage.setTitle("Monitor");
        primaryStage.setScene(new Scene(allHBox, 1060, 500));
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
